import { Buffer } from 'buffer'
import Api from './api'
import Address from './address'
import * as Config from '../../config/Config'
import { getCache, setCache, removeCache } from '../common/storageUtils'
import * as ignoreConfig from '../common/ignoreConfig'

/**
 * 初始化用户信息
 */
const initUserInfo = async() =>
{
  let token = await getCache(Config.TOKEN_KEY)
  // console.log(token)
  let res = await getUserInfoLocal()
  return {
    result: res.result && (token !== null),
    data: res.data,
  }

}


/**
 * 获取本地登录用户信息
 */
const getUserInfoLocal = async() =>
{
  let userText = await getCache(Config.USER_INFO)
  if (userText) {
    let res = JSON.parse(userText)
    return {
      result: true,
      data: res,
    }
  } else {
    return {
      result: false,
    }
  }
}


/**
 * 获取本地登录用户信息
 */
const cleanUserInfoLocal = async()=>
{
  await removeCache(Config.USER_INFO)
  return true
}


/**
 * 登录
 */
const doLogin = async(mobile, password) =>{
  console.log('mobile ' + mobile + ' pw ' + password)
  // let base64Str = Buffer(userName + ":" + password).toString('base64');
  await setCache(Config.USER_NAME_KEY, mobile)
  // await setCache(Config.USER_BASIC_CODE, base64Str);
  let requestParams = {
    mobile: mobile,
    password: password,
  }
  Api.clearAuthorization()
  let res = await
  Api.netFetch(Address.getAuthorization(), 'POST', requestParams, true)
  if (res && res.data) {
    setCache(Config.TOKEN_KEY, `Bearer ${res.data.success.token}`)
    setCache(Config.PW_KEY, password)
  }
  return res
}
/**
 * 注册接口
 * @param mobile
 * @param password
 * @returns {Promise<*>}
 */
const doRegiter = async(mobile, password, passwordConfirmation, confirmationCode) =>{
  console.log('mobile ' + mobile + ' pw ' + password + ' pwc ' + passwordConfirmation + ' code ' + confirmationCode)
  // let base64Str = Buffer(userName + ":" + password).toString('base64');
  await setCache(Config.USER_NAME_KEY, mobile)
  // await setCache(Config.USER_BASIC_CODE, base64Str);
  let requestParams = {
    mobile: mobile,
    password: password,
    'confirmation-code': confirmationCode,
    'password_confirmation': passwordConfirmation,
  }
  Api.clearAuthorization()
  let res = await
    Api.netFetch(Address.doRegister(), 'POST', requestParams, true)
  if (res && res.data) {
    setCache(Config.TOKEN_KEY, `Bearer ${res.data.success.token}`)
    setCache(Config.PW_KEY, password)
  }
  return res
}
/**
 * 重置密码
 */
const doRest = async(mobile, password, confirmationCode, passwordConfirmation) =>{
  console.log('mobile ' + mobile + ' pw ' + password + ' pwc ' + passwordConfirmation + ' code ' + confirmationCode)
  let requestParams = {
    mobile: mobile,
    password: password,
    'confirmation-code': confirmationCode,
    'password_confirmation': passwordConfirmation,
  }
  Api.clearAuthorization()
  let res = await Api.netFetch(Address.doRest(), 'POST', requestParams, true)

  return res
}
/**
 * 发送验证码
 */
const sendCode = async(mobile) => {
  console.log('mobile ' + mobile )
  let requestParams = {
    mobile: mobile,
  }
  // Api.clearAuthorization()
  let res = await Api.netFetch(Address.sendCode(), 'POST', requestParams, true)
  if (res && res.data) {
    console.log(res)
  }
  return res
}

/**
 * 获取用户详细信息
 */
const getUserInfoDao = async () =>
{
  let res
  res = await Api.netFetch(Address.getUserInfo())
  if (res &&  res.data.data) {
    let totalInfo = res.data.data
    totalInfo.login = true
    setCache(Config.USER_INFO, JSON.stringify(totalInfo))
    return {
      result: true,
      data: totalInfo,
    }
  } else {
    return {
      result: false,
      data: res.data.data,
    }
  }
}

/**
 * 获取用户今日学习分钟数
 */
const getUserDayMinute = async () => {

  let url = Address.getUserDayMinute()
  let res = await Api.netFetch(url)
  return res
}


/**
 * 获取用户本月已学时间
 */
const getUserMonthMinute = async () => {
  let url = Address.getUserMonthMinute()
  let res = await Api.netFetch(url)
  return res
}
/**
 * 历史总计
 */
const getUserTotalMinute = async () => {
  let url = Address.getUserTotalMinute()
  let res = await Api.netFetch(url)
  return res
}
/**
 * 获取未开始课程
 * @returns {Promise<*>}
 */
const getUserLessonsTimeAxis = async () => {
  let url = Address.getUserLessonsTimeAxis()
  let res = await Api.netFetch(url)
  return res
}
/**
 * 获取用户下一节课
 * @returns {Promise<*>}
 */
const getUserNextLesson = async () => {
  let url = Address.getUserNextLesson()
  let res = await Api.netFetch(url)
  return res
}
/**
 * 修改昵称
 * @param param
 * @returns {Promise<*>}
 */
const updateUserName = async (param) => {
  let res = await Api.netFetch(Address.updateUserName(), 'POST', param, true)
  return res
}
/**
 * 修改手机号
 * @param param
 * @returns {Promise<*>}
 */
const updateUserMobile = async (param) => {
  let res = await Api.netFetch(Address.updateUserMobile(), 'POST', param, true)
  return res
}

export default {
  doLogin,
  sendCode,
  doRest,
  getUserInfoDao,
  initUserInfo,
  getUserInfoLocal,
  getUserDayMinute,
  getUserMonthMinute,
  cleanUserInfoLocal,
  getUserTotalMinute,
  getUserLessonsTimeAxis,
  getUserNextLesson,
  updateUserName,
  updateUserMobile
}
